// Copyright 2007 Google Inc.
// All Rights Reserved.

package com.google.jessewilson.codesearch.gtagssearch;

import com.google.jessewilson.codesearch.api.SearchProvider;
import com.google.jessewilson.codesearch.api.spi.SearchProviderConfigurationEditor;
import com.google.jessewilson.codesearch.configuration.SearchProviderConfiguration;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import javax.swing.*;

/**
 * UI for editing Gtags configuration.
 *
 * @author Ted Mao (tmao@google.com)
 */
class GtagsConfigurationEditor implements SearchProviderConfigurationEditor {

  /**
   * @see GtagsConfiguration#executable
   */
  private final JTextField executable;

  /**
   * The panel to display in the configuration UI
   */
  private JPanel panel;

  public GtagsConfigurationEditor() {
    executable = new JTextField(5);

    FormLayout formLayout = new FormLayout(
        "right:min, 3px, left:pref:grow", "min");
    CellConstraints cc = new CellConstraints();

    panel = new JPanel(formLayout);
    panel.add(new JLabel("Command:"), cc.xy(1, 1));
    panel.add(executable, cc.xy(3, 1, "fill, default"));
  }


  public void setValue(SearchProviderConfiguration value) {
    GtagsConfiguration configuration = (GtagsConfiguration) value;
    executable.setText(configuration.getExecutable());
  }

  public SearchProviderConfiguration getValue() {
    return new GtagsConfiguration.Builder()
        .executable(executable.getText())
        .build();
  }

  public SearchProvider getSearchProvider() {
    return new GtagsSearchProvider();
  }

  public JComponent getComponent() {
    return panel;
  }
}
